//
// Created by amon.huang on 2021/7/25.
//

#ifndef AMONPLAYER_AUDIO_H
#define AMONPLAYER_AUDIO_H

#include <pthread.h>
#include <SLES/OpenSLES.h>
#include <cassert>
#include <SLES/OpenSLES_Android.h>
#include "JNICall.h"
#include "PacketQueue.h"
#include "PlayerStatus.h"

#include "MediaBase.h"

#ifdef __cplusplus
extern "C" {
#endif

#include <libavformat/avformat.h>
#include <libswresample/swresample.h>

#ifdef __cplusplus
}
#endif

class Audio : public MediaBase {
public:
    SLPlayItf pPlayItf;
    SwrContext *pSwrContext = NULL;
    uint8_t *resampleOutBuffer = NULL;
    int sampleRate;

    bool shouldStop;
    pthread_t playThreadT;
    pthread_mutex_t seekMutex;

public:
    Audio(int audioStreamIndex, JNICall *pJniCall, PlayerStatus *pPlayerStatus);

    ~Audio();

    void release();

    void play();

    void pause();

    int resampleAudio();

    void createOpenSLES();

    void seek(uint64_t second);

private:
    virtual void analysisStreamInner(ThreadMode threadMode, AVFormatContext *pFormatContext);

};


#endif //AMONPLAYER_AUDIO_H
